<template>
  <Manage>
    <template #upCard>
      <el-form label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="运单编号">
              <el-input v-model="page.id" placeholder="请输入运单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单编号">
              <el-input v-model="page.orderId" placeholder="请输入订单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="调度状态">
              <!-- <el-input v-model="page.schedulingStatus" placeholder="请输入快递员手机号" /> -->
              <el-select v-model="page.schedulingStatus" placeholder="请选择" style="width:100%">
                <el-option
                  v-for="item in setMap.schedulingStatus"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起始地机构">
              <Treeselect
                v-model="page.startAgencyId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择起始地机构"
                :append-to-body="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目的地机构">
              <Treeselect
                v-model="page.endAgencyId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择目的地机构"
                :append-to-body="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="当前所在机构">
              <Treeselect
                v-model="page.currentAgencyId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择当前所在机构"
                :append-to-body="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" size="" @click="search">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <template #midCard>
      <TabChange :tabs="setMap.transportOrderTabs" :status-count="statusCount" @changeItem="changeItem" />
    </template>
    <template #downCard>
      <el-table
        v-loading="loading"
        :data="transportData"
        stripe
        :header-cell-style="{background:'#f8faff',color:'#818693'}"
      >
        <el-table-column type="index" label="序号" />
        <el-table-column prop="id" label="运单编号" />
        <el-table-column prop="orderId" label="订单编号" />
        <el-table-column prop="status" label="运单状态">
          <template #default="{row}">
            <span>{{ row.status || showTransportStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="schedulingStatus"
          label="调度状态"
          :formatter="(row)=>setMap.schedulingStatus.find(item=>item.id===row.schedulingStatus).label"
        />
        <el-table-column prop="startAgencyName" label="起始营业部" />
        <el-table-column prop="endAgencyName" label="终点营业部" />
        <el-table-column prop="currentAgencyName" label="当前所在机构" />
        <el-table-column prop="nextAgencyName" label="下一个机构" />
        <el-table-column prop="totalVolume" label="货品总体积(立方米)" />
        <el-table-column prop="totalWeight" label="货品总重量(千克)" />
        <el-table-column prop="created" label="创建时间" />
        <el-table-column prop="updated" label="更新时间" />
        <el-table-column label="操作" align="center" width="100px" fixed="right">
          <template #default="{row}">
            <!-- <a href="javascript:void(0);" style="color:#419eff;">作业范围分配</a> -->
            <router-link
              style="color:#419eff;"
              :to="{path:'/business/waybill-detail',
                    query:{
                      id:row.id,
                      orderId:null
                    }
              }"
            >查看详情</router-link>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </template>
  </Manage>
</template>

<script>
import { getOrgizationTree } from '@/api/ogization'
import { getStatusCount, getTransportOrder } from '@/api/business'
import setMap from '@/constant/setMap'
export default {
  filters: {
    showTransportStatus(val) {
      return setMap.tansportStatus.find(item => item.id === val)?.label || ''
    }
  },
  data() {
    return {
      setMap,
      page: {
        page: 1,
        pageSize: 10,
        id: null,
        orderId: null,
        schedulingStatus: null,
        startAgencyId: null,
        endAgencyId: null,
        currentAgencyId: null
      },
      status: null,
      statusCount: {},
      counts: 0,
      tree: [],
      transportData: [],
      loading: false
    }
  },
  created() {
    this.initTree()
    this.initTransportOrder()
    this.initStatusCount()
  },
  methods: {
    async initStatusCount() {
      this.statusCount = await getStatusCount()
    },
    // 快递员管理表格
    async initTransportOrder() {
      this.loading = true
      try {
        const res = await getTransportOrder({ ...this.page, status: this.status })
        this.transportData = res.items
        this.counts = +res.counts
      } catch (error) {
        this.loading = false
      }
      this.loading = false
    },
    // 切换
    changeItem(id) {
      this.status = id
      this.initTransportOrder()
    },
    // 下拉树结构
    async initTree() {
      const res = await getOrgizationTree()
      this.tree = JSON.parse(res)
    },
    normalizer(node) {
      // 当子节点也就是children=[]时候去掉子节点
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children && node.children.length > 0 ? node.children : 0
      }
    },
    // 搜索
    search() {
      this.initTransportOrder()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageSize = val
      this.initTransportOrder()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.initTransportOrder()
    },
    reset() {
      this.page = {
        page: 1,
        pageSize: 10,
        id: null,
        orderId: null,
        schedulingStatus: null,
        startAgencyId: null,
        endAgencyId: null,
        currentAgencyId: null
      }
      this.initTransportOrder()
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
